﻿/*
 * 2023/2/24
 * 城市列表控制器
 * 只有一个查询的方法（基础数据，不让添加、修改、删除）
 * 
 * **/
using Max.PH5_2008A.ShoppingCode.Domain;
using Max.PH5_2008A.ShoppingCode.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace Max.PH5_2008A.ShoppingCode.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CityInfoController : ControllerBase
    {
        ICityInfoService _service;

        public CityInfoController(ICityInfoService service)
        {
            _service = service;
        }


        /// <summary>
        /// 查询城市列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Query()
        {
            Stopwatch a = new Stopwatch();
            a.Start();
            //调用服务，获取数据
            var list = _service.Query().ToList();
            var data = this.RecurrenceCity(list,1); //_service.Query();
            a.Stop();
            Console.WriteLine(a.ElapsedMilliseconds); 
            //返回值
            return Ok(data);

        }


        private List<dynamic> RecurrenceCity(List<CityInfoEntity> data ,int id)
        {
            var list = data.Where(x => x.PId == id).ToList();
            List<dynamic> result = new List<dynamic>();
            list.ForEach(x =>
            {
                var s = RecurrenceCity(data, x.Id);

                result.Add(new
                {
                    value = x.Id,
                    label = x.Name,
                    children = s == null || s.Count<=0 ? null : s
                });
            });
            return result;
        }
    }
}



public class UserInfo
{
    public void Add() { }
    public void Update() { }


    ////2023/1/23 添加删除方法
    //public void Delete() { }

}

public static class UserInfoExtends 
{
    public static void Delete(this UserInfo user) 
    { }
}
